package com.heu.blood.storge.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heu.blood.storge.entity.BloodStorgeWarningEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.heu.blood.storge.service.BloodStorgeWarningService;
import com.heu.blood.storge.vo.BloodStorgeWarningVo;
import com.heu.blood.storge.vo.BloodWarningVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * 
 * 
 * @author wjh
 * @email 1208325290@heu.com
 * @date 2023-07-25 11:58:46
 */
@Mapper
public interface BloodStorgeWarningDao extends BaseMapper<BloodStorgeWarningEntity> {
    @Select("select o.abo_blood_type,z.dict_value as aboTypeCode,count(*) as residueBloodBag,u.blood_warning_value,o.is_deleted from blood_information as o left join blood_storge_warning as u on o.abo_blood_type = u.abo_type_code left join dict_data as z on o.abo_blood_type = z.dict_code where o.is_deleted=0 and o.if_show=1 and o.blood_state= \"IN_STOCK\" and z.dict_type = \"AboType\" group by o.abo_blood_type having count(*) <= u.blood_warning_value order by o.failure_time desc ")
    //@Select("select o.abo_blood_type,u.abo_type_code,count(*) as residueBloodBag from blood_information as o left join blood_storge_warning as u on o.abo_blood_type =u.abo_type_code group by o.abo_blood_type having count(*) > u.blood_warning_value")
    IPage<BloodStorgeWarningVo> warningSelect(IPage<BloodStorgeWarningVo> page);


    //@Select("select DISTINCT o.abo_blood_type,z.dict_value as aboTypeCode,count(*) as residueBloodBag,u.blood_warning_value from blood_information as o left join blood_storge_warning as u on o.abo_blood_type = u.abo_type_code left join dict_data as z on o.abo_blood_type = z.dict_code group by o.abo_blood_type ")
    @Select("select o.abo_blood_type,z.dict_value as aboTypeCode,count(*) as residueBloodBag,u.blood_warning_value from blood_information as o left join blood_storge_warning as u on o.abo_blood_type = u.abo_type_code left join dict_data as z on o.abo_blood_type = z.dict_code where o.failure_time<=DATE_ADD(NOW(), INTERVAL u.time_warning_value DAY) and o.is_deleted=0 and o.if_show=1 and o.failure_time>=NOW() and z.dict_type = \"AboType\" group by o.abo_blood_type order by o.failure_time desc ")
    IPage<BloodStorgeWarningVo> warningSelectByTime(IPage<BloodStorgeWarningVo> page);

    @Select("select o.*,TIMESTAMPDIFF(day,o.failure_time,NOW())+1 as timeoutDays from blood_information as o where TIMESTAMPDIFF(day,o.failure_time,NOW())>=0 and o.is_deleted=0 and o.if_show=1 and o.blood_state= \"IN_STOCK\" order by o.failure_time DESC,timeoutDays DESC ")
    IPage<BloodWarningVo> newWarningSelectByTime(IPage<BloodWarningVo> page);


}
